package com.sw.small_world.fragment.home;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.sw.small_world.R;
import com.sw.small_world.activity.MainActivity;
import com.sw.small_world.fragment.base.FragmentFactory;
import com.sw.small_world.utils.ShareUtils;

/**
 * Created by Administrator on 2017/3/18.
 */

public class BaseHomeFragment extends Fragment {

    private MainActivity activity;
    private View view;
    private FragmentManager fm;
    private FragmentTransaction ft;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (MainActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(activity, R.layout.fragment_base_home, new RelativeLayout(activity));
        setCurrentFragment(0);
        return view;
    }

    public void setCurrentFragment(int position) {
        fm = getFragmentManager();
        ft = fm.beginTransaction();
        ft.add(R.id.home_rl_container, FragmentFactory.createForHome(position), position + "");
        if (position != 0) {
            ft.addToBackStack(null);
            activity.homeSearch.setVisibility(View.GONE);

        } else {
            activity.homeSearch.setVisibility(View.VISIBLE);
        }
        ShareUtils.putInt(ShareUtils.IS_HOME,position);
        ft.commit();
    }
}
